import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:myapp/app/theme/app_color.dart';

class ProfileHistoryLayout extends StatefulWidget {
  final List<String> tabs = ["放映厅", "专栏", "小说"];

  final String movie =
      "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13926895099%2F641&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1632797230&t=b5cfeaf64f4237bebd957a536dfc3846";

  final length = 3;

  final EdgeInsets? margin;

  ProfileHistoryLayout({this.margin});

  @override
  _ProfileHistoryLayoutState createState() => _ProfileHistoryLayoutState();
}

class _ProfileHistoryLayoutState extends State<ProfileHistoryLayout> {
  @override
  void initState() {
    super.initState();
  }

  Widget _itemPage() {
    return Container(
      margin: EdgeInsets.only(top: 15),
      width: double.infinity,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemBuilder: (context, index) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(right: 15),
                height: 110,
                child: AspectRatio(
                  aspectRatio: 16.0 / 5,
                  child: ClipRRect(
                    child: Image.network(
                      widget.movie,
                      fit: BoxFit.cover,
                    ),
                    borderRadius: BorderRadius.circular(3),
                  ),
                ),
              ),
              Text(
                "\n回家的路有多远 第1集",
                style: TextStyle(fontSize: 14, color: AppColor.hex_999),
              ),
            ],
          );
        },
        itemCount: 3,
      ),
    );
  }

  Widget _buildTabLayout() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TabBar(
          isScrollable: true,
          indicator: BoxDecoration(),
          labelColor: AppColor.hex_333,
          labelPadding: EdgeInsets.only(left: 10, right: 10),
          unselectedLabelColor: AppColor.hex_ddd,
          tabs: widget.tabs
              .map((e) => Text(e, style: TextStyle(fontSize: 16)))
              .toList(),
        ),
        AspectRatio(
          aspectRatio: 16.0 / 7,
          child: TabBarView(
            children: [
              _itemPage(),
              _itemPage(),
              _itemPage(),
            ],
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: widget.margin,
      decoration: BoxDecoration(
        color: AppColor.white,
        borderRadius: BorderRadius.circular(5),
      ),
      padding: EdgeInsets.fromLTRB(10, 10, 0, 10),
      width: double.infinity,
      child: Stack(children: [
        DefaultTabController(
          child: _buildTabLayout(),
          length: widget.length,
        ),
        Positioned(child: Text("全部 >", style: TextStyle(fontSize: 14, color: AppColor.hex_ddd),), top: 3, right: 10,),
      ],),
    );
  }
}
